package com.hfzy.ihk.web.userCenter.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hfzy.ihk.facade.userCenter.entity.Resource;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 用于集成spring security,权限表 Mapper 接口
 * </p>
 *
 * @author wws
 * @since 2018-12-28
 */
public interface ResourceMapper extends BaseMapper<Resource> {

    @Select("select * from resource t where t.label=#{resourceName} and t.type<>2")
    Resource getResourceByName(String resourceName);

    @Select("select * from resource t where t.btn_permission=#{btnPermissionName}")
    Resource getResourceBybtnPremissionName(String btnPermissionName);

    @Select("select * from resource t order by t.priority")
    List<Resource> listAll();

    @Select("select * from resource t where t.id = #{id}")
    Resource getById(Long id);



    @Select("select * from resource t where t.parent_id=0 order by t.priority " )
    List<Resource> listTopParents();

    @Select("select * from resource t where  t.type=1  order by t.priority ")
    List<Resource> listParents();

    @Select("select * from resource t order by t.priority ")
    List<Resource> allListParents();

    @Update("update resource t set parent_Id = #{parentId}, label = #{label}, css = #{css}, uri = #{uri}, type = #{type},  priority = #{priority} where t.id = #{id}")
    int update(Resource resource);


    @Delete("delete from resource where id = #{id}")
    int delete(Long id);

    @Insert("insert into resource(parent_Id, label, css, uri, type, priority) values(#{parentId}, #{label}, #{css}, #{uri}, #{type},  #{priority})")
    int save(Resource resource);

    @Delete("delete from user_has_resource where resource_id = #{id}")
    int deleteUserHasResourceByResourceId(Long id);

    @Delete("delete from priv_has_resource where resource_id = #{id}")
    int deletePrivHasResourceByResourceId(Long id);


    @Select("select DISTINCT hr.resource_id  from user_has_resource  hr LEFT JOIN resource r on r.id =hr.resource_id where hr.user_id=#{userId}")
    List<Long> userHasResource(Long userId);

    @Select("select DISTINCT r.resource_id from priv_has_resource r "
            +" LEFT JOIN priv p on p.id=r.priv_id "
            +" LEFT JOIN role_has_priv pi on pi.priv_id=p.id"
            +" LEFT JOIN user_has_role ur ON ur.role_id=pi.role_id "
            +" where ur.user_id=#{userId}")
    List<Long> privHasResourceByUserId(Long userId);

}
